package com.itheima.api;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.UmsAdminMapper;
import com.itheima.pojo.UmsAdmin;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class UmsAdminServiceImpl implements UmsAdminServiceApi{

    @Autowired
    private UmsAdminMapper umsAdminMapper;

    /**
     * 根据用户名查找用户
     * @param username
     * @return
     */
    @Override
    public UmsAdmin findByName(String username) {
        LambdaQueryWrapper<UmsAdmin> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UmsAdmin::getUsername,username);
        return umsAdminMapper.selectOne(wrapper);
    }

    /**
     * 根据id查找用户
     * @param userId
     * @return
     */
    @Override
    public UmsAdmin getAdminById(Long userId) {
        LambdaQueryWrapper<UmsAdmin> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UmsAdmin::getId,userId);
        return umsAdminMapper.selectOne(wrapper);
    }

    /**
     * 分页查询用户信息
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public Page<UmsAdmin> findByPage(Integer pageNum, Integer pageSize, String keyword) {
        //构造查询条件
        Page<UmsAdmin> pageInfo=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<UmsAdmin> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(keyword),UmsAdmin::getUsername,keyword);
        return umsAdminMapper.selectPage(pageInfo,wrapper);
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @Override
    public void deleteById(Integer id) {
        umsAdminMapper.deleteById(id);
    }

    /**
     * 添加用户
     * @param umsAdmin
     * @return
     */
    @Override
    public void saveUms(UmsAdmin umsAdmin) {
        umsAdminMapper.insert(umsAdmin);
    }

    /**
     * 修改用户
     * @param id
     * @param umsAdmin
     */
    @Override
    public void updateById(Integer id, UmsAdmin umsAdmin) {
        LambdaQueryWrapper<UmsAdmin> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UmsAdmin::getId,id);
        umsAdminMapper.update(umsAdmin,wrapper);
    }

    /**
     * 修改用户状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Integer id, Integer status) {
        LambdaQueryWrapper<UmsAdmin> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UmsAdmin::getId,id);
        UmsAdmin umsAdmin = new UmsAdmin();
        umsAdmin.setStatus(status);
        umsAdminMapper.update(umsAdmin,wrapper);
    }

    /**
     * 根据用户id查找用户
     * @param id
     * @return
     */
    @Override
    public UmsAdmin findById(Integer id) {
        LambdaQueryWrapper<UmsAdmin> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UmsAdmin::getId,id);
        return umsAdminMapper.selectOne(wrapper);
    }
}
